package store

import (
	"guard/internal/app/admin/models"
	"guard/internal/db"
	"guard/internal/tool"
)

func (s *adminStoreImpl) SelScopePage(current, size int) *tool.Paginator {
	/*
		var (
			permission  []models.AuthorityPermission
			total int
		)

		// 分页结构体
		paginator := &tool.Paginator{
			Page:     current,
			PageSize: size,
		}

		if err := s.db.Offset(paginator.Offset()).Limit(size).Find(&permission).Count(&total).Error; err != nil {
			return nil
		}

		paginator.Total = total
		paginator.Data = role

		return paginator
	*/
	return db.SelPage(&models.AuthorityScope{}, current, size, s.db)
}

func (s *adminStoreImpl) SelScopeQuery(tag, permission string) (as *models.AuthorityScope, err error) {
	err = s.db.Where("tag = ?", tag).Or("permission = ?", permission).First(&as).Error
	return
}

func (s *adminStoreImpl) SaveOrUpdateScope(scope models.AuthorityScope) error {
	// 创建，更新，都在这个保存方法里了。。。都写好了的
	return s.db.Save(&scope).Error
}

func (s *adminStoreImpl) DelScope(tag string) error {
	return s.db.Exec("delete from oauth2_scope where tag = ?", tag).Error
}

func (s *adminStoreImpl) SelScopeAll() (mas []models.AuthorityScope, err error) {
	err = s.db.Find(&mas).Error
	return
}
